package com.lej.core.utils.extention

import android.animation.Animator
import android.animation.ObjectAnimator
import android.app.Activity
import android.content.ContextWrapper
import android.graphics.Color
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.view.View
import android.view.ViewGroup
import android.view.animation.LinearInterpolator
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.constraintlayout.widget.Group
import androidx.core.animation.addListener
import androidx.core.view.isVisible
import com.lej.core.R
import com.qmuiteam.qmui.kotlin.onClick
import com.qmuiteam.qmui.span.QMUITouchableSpan

fun Group.setAllOnClickListener(listener: (View) -> Unit) {
    referencedIds.forEach { id ->
        (this.parent as? ViewGroup)?.findViewById<View>(id)?.onClick(block = listener)
    }
}

fun View.getActivity(): Activity? {
    var context = context
    while (context is ContextWrapper) {
        if (context is Activity) return context
        context = context.baseContext
    }
    return null
}

fun View.fadeShow(endRun: () -> Unit = {}) {
    isVisible = true
    alpha = 0f
    animate().alpha(1f).setDuration(200).withEndAction(endRun).start()
}

fun View.fadeHide() {
    animate().alpha(1f).setDuration(200).withEndAction {
        isVisible = false
    }.start()
}

fun View.transLate(
    linearInterpolator: LinearInterpolator = LinearInterpolator(),
    duration: Long = 200L,
    distance: Float = 0F
) {
    animate().setInterpolator(linearInterpolator)
        .setDuration(duration)
        .translationY(distance)
}


fun TextView.textLinkClick(text: String, block: () -> Unit = {}): SpannableString {
    val textView = this
    textView.movementMethod = LinkMovementMethod.getInstance()
    return SpannableString(text).apply {
        setSpan(object : QMUITouchableSpan(
            textView,
            R.attr.app_click_text_color,
            R.attr.app_click_text_color,
            0,
            0
        ) {
            override fun onSpanClick(widget: View) {
                block()
            }
        }, 0, text.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
    }
}

fun CharSequence.textLinkClick(
    @ColorRes color: Int = R.color.white,
    block: () -> Unit = {}
) =
    SpannableString(this).apply {
        val textColor = color.resColor
        setSpan(object :
            QMUITouchableSpan(textColor, textColor, Color.TRANSPARENT, Color.TRANSPARENT) {
            override fun onSpanClick(widget: View) {
                block()
            }
        }, 0, length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
    }

/**
 * 目标textView需要手动设置textView.movementMethod = LinkMovementMethod.getInstance()
 * */
fun CharSequence.appendLinkClick(text: CharSequence, block: () -> Unit = {}) =
    SpannableStringBuilder(this).append(SpannableString(text).apply {
        setSpan(object : QMUITouchableSpan(
            R.color.themeColorAccent.resColor,
            R.color.themeColorAccent.resColor,
            0,
            0
        ) {
            override fun onSpanClick(widget: View) {
                block()
            }
        }, 0, text.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
    })

private fun fadeAnim(view: View) =
    ObjectAnimator.ofFloat(view, "alpha", 1f, 0f).apply { duration = 250 }

private fun showAnim(view: View) =
    ObjectAnimator.ofFloat(view, "alpha", 0f, 1f).apply { duration = 250 }


fun View.fadeAnim(duration: Long = 200) {
    ObjectAnimator.ofFloat(this, "alpha", 1f, 0f)
        .also { it.duration = duration }
        .start()
}

fun View.bottomEnterAnim(duration: Long = 200) {
    ObjectAnimator.ofFloat(this, "translationY", height.toFloat(), 0f)
        .also { it.duration = duration }
        .start()
}

fun View.bottomExitAnim(duration: Long = 200, block: (Animator) -> Unit) {
    ObjectAnimator.ofFloat(this, "translationY", 0f, height.toFloat())
        .setDuration(duration)
        .also { it.addListener(onEnd = block) }
        .start()
}


fun View.rightEnterAnim(duration: Long = 200) {
    ObjectAnimator.ofFloat(this, "translationX", width.toFloat(), 0f)
        .also { it.duration = duration }
        .start()
}

fun View.rightExitAnim(duration: Long = 200, block: (Animator) -> Unit) {
    ObjectAnimator.ofFloat(this, "translationX", 0f, width.toFloat())
        .setDuration(duration)
        .also { it.addListener(onEnd = block) }
        .start()
}